package sql

import (
	"fmt"
	"pdocker/config"

	log "github.com/sirupsen/logrus"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

var (
	PgDB *gorm.DB
)

func init() {
	fmt.Println(config.Config.Database.Host)
	PgDB = initPostGre()

}

func initPostGre() *gorm.DB {
	// Generate DSN for GORM
	dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
		config.Config.Database.Host,
		config.Config.Database.Username,
		config.Config.Database.Password,
		config.Config.Database.DBName,
		config.Config.Database.Port,
		config.Config.Database.SSLMode,
	)

	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalf("Error connecting to database: %v", err)
	}

	// Close database connection when done

	return db
}
